package com.test;


import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @Description:  java调用python接口
 * @Author hugh.sun
 * @Date 2021/11/23 11:13
 */

public class RuntimePython {

    @Test
    public void test1()
            {
                try {
                    String[] arguments = new String[] {"python", "E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\test1.py", "hello", "25"};

                    Process process = Runtime.getRuntime().exec(arguments);
                    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                    in.close();
                    int re = process.waitFor();
                    System.out.println(re);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            @Test
            public void test2()
            {
        int a = 111;
        int b = 222;

        try {
            String[] str = new String[]{"python","E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\test1.py",String.valueOf(a),String.valueOf(b)};
            Process exec = Runtime.getRuntime().exec(str);
            BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
            String line = null;
            System.out.println("-----------------");
            while ((line = in.readLine()) != null) {
                System.out.println("------------");
                System.out.println(line+"---------------");


            }
            in.close();
            try {
                int re = exec.waitFor();
                System.out.println(re);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    @Test void test3() throws IOException, InterruptedException {
        String exe = "python";
        String command = "E:\\项目-estore\\day70\\springCloud\\sprintboot-redis\\src\\main\\resources\\test2.py";
        String num1 = "1";
        String num2 = "2";
        String[] cmdArr = new String[] {exe, command,num1, num2};
        Process process = Runtime.getRuntime().exec(cmdArr);
        InputStream is = process.getInputStream();



        System.out.println(is);
        DataInputStream dis = new DataInputStream(is);
        int read = dis.read();
        String str = dis.readLine();
        System.out.println(read);
        process.waitFor();
        System.out.println(str);
    }




    }


